<%--
  Created by IntelliJ IDEA.
  User: hklyq
  Date: 2020-01-02
  Time: 14:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>资方贷后数据上传</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
    <jsp:include page="" flush="true" /><!-- 时间格式转换 -->
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div class="searchArea">
        <!-- 搜索表单开始 -->
        <form id="searchBar">
            <div class="fieldset1 clearFix">
                <div class="FXBR">
                    <div class="fieldBox">
                        <div class="label">机构名称</div>
                        <div class="field">
                            <select name="insNm" id="insNm" ligerui="width: 160" >
                                <option value="  " selected>请选择</option>
                                <c:forEach items="${insCdNms}" var="ins">
                                    <option value="${ins.key}">${ins.value}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="fieldBox">
                        <div class="label">数据日期</div>
                        <div class="field">
                            <input type="text" id="dataDate" name="dataDate" class="ui-datepicker"
                                   ligerui="width: 120" value="">
                        </div>
                    </div>
                </div>
                <div class="clearFix"></div>
                <!-- 表单按钮 -->
                <div class="btnsBox formBtns notRight">
                    <span id="searchBtn" class="sq-button-round">查询</span>
                    <span id="clearBtn" class="sq-button-round">清空</span>
                    <div id="fileUpload1" class="upfileBoxs"></div>
                    <span class="sq-button sq-button-hasicon" id="btnUpload1" onclick="uploadFile('uploads1',1)">
								<i class="sqBtnIco_9"></i>上传
							</span>
                    <%--<span id="fileUpload1" class="upfileBoxs" onclick="uploadFile('uploads1', 1);">上传</span>--%>
                </div>
            </div>
        </form>
        <!-- 搜索表单结束 -->
    </div>
</div>

<!-- 列表显示块 -->
<div id="ligergrid1" class="sq-grid"></div>

<script type="text/javascript">
    var grid = null;
    $(function () {
        initForm();//初始化表单
        initGrid();//注意初始化要在initForm之后,因为在初始化grid的时候需要form参数
        searchBtnEvent();//查询按钮事件
        clearBtnEvent();//清空按钮事件
    });

    //初始化表单
    function initForm() {
        $("#searchBar").ligerForm();
    }

    //查询参数
    function getSearchParams() {
        var form = new liger.get("searchBar");
        var data = form.getData();
        return data;
    }

    //初始化列表
    function initGrid() {
        $("#ligergrid1").ligerGrid({
            url: '<%=path%>/capital/insFileSync/initList.do',
            method: 'get',
            parms: getSearchParams(),
            columns: [
                {display: '文件名称', name: 'fileName', width: 100, enabledSort: true},
                {
                    display: '文件编号', name: 'code', minWidth: 120, enabledSort: true,
                    render: function (rowdata) {
                        var h = '';
                        h='<a href="javascript:showDetail(';
                        h+="'"+rowdata.id+"'";
                        h+=')">'+rowdata.code+'</a>';
                        return h;
                    }
                },
                {
                    display: '资方机构名称', name: 'insName', width: 150, enabledSort: true
                },
                {
                    display: '数据日期', name: 'dataDate', width: 120, enabledSort: true,
                    render: function (rowdata, rowindex, value) {
                        var h = renderDate(rowdata.dataDate)
                        return h;
                    }
                },
                {display: '上传时间', name: 'upDate', width: 120, enabledSort: true,
                    render: function (rowdata, rowindex, value) {
                        var h = renderDate(rowdata.upDate)
                        return h;
                    }},
                {display: '操作人', name: 'crtNm', width: 120, enabledSort: true},
                {display: '上传状态', name: 'uploadStsNm', width: 120, enabledSort: true},
                {display: '解析状态', name: 'parseStsNm', width: 120, enabledSort: true},
                getGridOperColumnContent()//操作列
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: false,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            checkbox: true,//列表是否有选择框
            rownumbers: true//列表是否有行号
        });
        grid = liger.get("ligergrid1");
        grid.setHeight(100);
        grid.options.isSingleCheck = false;//列表是否为单选
    }

    // 操作列
    function getGridOperColumnContent() {
        var data = {
            display: '操作',
            isSort: false,
            width: 80,
            align: 'center',
            render: function (rowdata, rowindex, value) {
                var h = "";
                h += '<a class="sq-button-hasicon" alt="下载" href="javascript:downloadFile(';
                h += "'" + rowdata.fileId + "'";
                h += ")\"><i class=\"sqBtnIco_8\"></i>" + "下载" + "</a>";
                return h;
            }
        };
        return data;
    }

    // 下载文件
    function downloadFile (fileId) {
        var wait_dialog;
        $.ajax({
            type: 'post',
            url: '<%=path%>/capital/insFileSync/downloadFile.do',
            data: {fileId: fileId},
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: '正在下载...', SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    window.open(msg.data);
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    };

    // 清空按钮事件
    function clearBtnEvent() {
        $("#clearBtn").click(function (event) {
            var form = liger.get("searchBar");
            form.setData({  //重置select等特殊表单
                "insNm": "",
                "dataDate": ""
            });
        });
    }

    // 查询按钮事件
    function searchBtnEvent() {
        $("#searchBtn").click(function (event) {
            var form = new liger.get("searchBar");
            var data = form.getData();
            //alert(JSON.stringify(data));
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            ;
            grid.reload();
        });
    }

    // 格式化时间
    function renderDate(value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

    function showDetail(id) {
        $.ligerDialog.SQopen({
            width: 1100,
            height: 550,
            title: '贷后数据详情',
            url: '<%=path%>/capital/insFileSync/showInsFileSyncD.do?id=' + id
        });
    }
</script>

<script type="text/javascript">
    /**
     * 上传文件的js
     */

    //文件上传参数
    var upload_result_params1 = [];
    var serverPrefixPath = "";
    //页面加载
    $(function(){
        //添加上传组件
        $(".upfileBoxs").each(function(index, el){
            var num = el.id.split("fileUpload")[1];
            $(this).append(addUpfileBox(num));
        });
        // 获取服务器路径前缀
        $.ajax({
            type: 'get',
            url: '<%=path%>/system/sysulfilesrec/getServerPath.do',
            success:function(msg){
                serverPrefixPath = msg;
                console.log(serverPrefixPath);
            }
        });
    });

    //添加上传组件方法
    function addUpfileBox(num) {
        $Html=CreatUploadInputBox({
            id:"uploads" + num,  //id跟着序号走
            clearLine: true,
            deletable: false,
            finishClear: true //上传成功后清空文本框
        });
        return $Html;
    }

    var msgData;

    //上传方法
    function uploadFile(fileId, fileIndex){
        var _filePath = "JxBankFile";
        var wait_dialog = $.ligerDialog.SQalert({content: '上传中,请稍后...' ,SQ_type: "waitting" });
        //文件上传
        $.ajaxFileUpload({
            url:'<%=path%>/system/sysulfilesrec/upload.do',
            secureuri:false,
            fileElementId: fileId,  //上传框的id
            dataType: 'json',
            data: {filePath:_filePath},
            success: function (msg){
                wait_dialog.close();
                if(msg.success == 1){
                    msgData = msg.data;
                    // $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                    // 累计上传文件-json数组-数组合并
                    if (fileIndex == 1) {
                        upload_result_params1 = upload_result_params1.concat(msg.data);
                    }
                    //遍历msg.data为filePath加上服务器路径前缀
                    if(serverPrefixPath.length >1 && serverPrefixPath.indexOf("\"")>=0){
                        serverPrefixPath = serverPrefixPath.substring(1,serverPrefixPath.length-1);
                    }
                    //var uploadJOSN = msg.data;
                    var uploadJOSN = JSON.parse(JSON.stringify(msg.data));
                    for (var i=0; i< uploadJOSN.length; i++){
                        var oldPath = "";
                        var newPath = "";
                        oldPath = uploadJOSN[i].filePath;
                        newPath = serverPrefixPath + oldPath;
                        uploadJOSN[i].filePath = newPath;
                    }
                    //如果使用uploadJOSN = msg.data
                    //将直接改变upload_result_params的值-是指针导致的
                } else{
                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                }
            },
            complete: function(){
                console.log(JSON.stringify(msgData));
                var form = new liger.get("searchBar");
                var data = form.getData();
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: {
                        'fileParams': JSON.stringify(msgData),
                        'insNm':data.insNm
                    },
                    url: '<%=path%>/capital/insFileSync/uploadFile.do',
                    success: function (msg) {
                        if (msg.success == 1) {
                            grid.reload();
                        } else {
                            $.ligerDialog.error(msg.info);
                        }
                    }
                });
            }
        });
    }

    //获取上传结果参数
    function getUploadResultParams(fileIndex){
        // 根据序号返回不同的参数
        if (fileIndex == 1) {
            return upload_result_params1;
        }
    }

    //清除上传结果参数
    function clearUploadResultParams(fileIndex){
        // 根据序号清空不同的参数
        if (fileIndex == 1) {
            upload_result_params1 = [];
        }
    }

</script>
</body>
</html>
